package com.qfedu.mapper;

import com.qfedu.model.Branch;
import com.qfedu.model.Project;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * 概述:
 *
 * @Author: Tbh
 * @Date: 2022/7/1 14:15
 */
public interface BranchMapper {

    @Results(id = "branchMap",value = {
            @Result(column = "bid",property = "bid"),
            @Result(column = "bname",property = "bname"),
            @Result(column = "updatetime",property = "updatetime"),
            @Result(column = "pid",property = "pid"),
            @Result(column = "pid",property = "project",javaType = Project.class,one =  //一对一关联查询
            @One(select = "com.qfedu.mapper.ProjectMapper.findProject",fetchType = FetchType.EAGER)
            ),
    })
    @Select("select * from branch where status = 1 limit #{currentPage},#{pageSize}")
    List<Branch> findPage(@Param("currentPage") int currentPage, @Param("pageSize") Integer pageSize);

    @Select("Select count(*) from branch where status=1")
    Long count();

    @Insert("insert into branch (bid,bname,updatetime,pid) values (#{bid},#{bname},#{updatetime},#{pid})")
    int addBranch(Branch branch);

    @ResultMap("branchMap")
    @Select("select * from branch where bid = #{bid} and status = 1")
    Branch findByBid(String bid);

    @Update("update branch set bname = #{bname},pid = #{pid} where bid = #{bid} and status = 1")
    int updateBranch(Branch branch);

    @Update("update branch set status = 0 where bid = #{bid}")
    int deleteByBid(String bid);

    @Select("select * from branch where status = 1")
    List<Branch> findAll();
}
